package com.invengo.testrfid.app.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.invengo.testrfid.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyDialogueUtils {

    private AlertDialog alertDialog;

    private TextView tv_diaTitle;
    private TextView btn_cancel;
    private TextView btn_ok;
    private TextView tv_body;

    private View view;

    private Context context;

    private Button btn_left;
    private Button btn_right;

    private RelativeLayout re_head;
    private RelativeLayout re_foot;
    private RelativeLayout re_back;

    // 输入框字符
    private String textData;

    /**
     * 弹出对话框的类别
     */
    private int alertDialogType;
    private int textLong;

    /**
     * 弹出框标识码
     */
    private int code;
    private TextView tv_des;
    private String judgeText = "no";
    private MyDialogueUtilsListenser listenser;
    /**
     * 显示加载对话框
     * <p>
     * 使用方法，直接初始化就可以了。
     */

    private ProgressBar progressBar;

    // ================================Show=====================================
    private TextView tv_complete_percent;

    public MyDialogueUtils(Context context) {
        this(context, 0);
        this.context = context;
    }


    public MyDialogueUtils(Context context, int alertDialogType) {
        this.alertDialogType = alertDialogType;
        this.context = context;
        alertDialog = new AlertDialog.Builder(context).create();
        view = View.inflate(context, R.layout.common_dialog, null);
        re_head = (RelativeLayout) view.findViewById(R.id.re_title);
        re_foot = (RelativeLayout) view.findViewById(R.id.re_foot);
        re_back = (RelativeLayout) view.findViewById(R.id.re_back);
        tv_diaTitle = (TextView) view.findViewById(R.id.tv_diaTitle);
        tv_body = (TextView) view.findViewById(R.id.tv_body);
        btn_cancel = (TextView) view.findViewById(R.id.btn_cancel);
        btn_ok = (TextView) view.findViewById(R.id.btn_ok);
        re_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        setButtonClick();
    }

    public MyDialogueUtils(Context context, int alertDialogType,
                           MyDialogueUtilsListenser listenser) {
        this(context, alertDialogType);

        registerListenser(listenser);
        setButtonClick();
    }

    public MyDialogueUtils(Context context, int alertDialogType,
                           MyDialogueUtilsListenser listenser, int code) {
        this(context, alertDialogType, listenser);
        this.code = code;
    }

    public void showDia() {
        alertDialog.setView(view);
        alertDialog.show();
    }

    public void showDiaNoCancel() {
        alertDialog.setCancelable(false);
        showDia();
    }

    public void showProgressDialogue() {
        view = View.inflate(context, R.layout.common_dia_progress, null);
        showDiaNoCancel();
    }

    /**
     * 设置
     */
    public void showProgressHoriDialogue() {

        view = View.inflate(context, R.layout.common_dia_hori_progress, null);
        progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        tv_complete_percent = (TextView) view.findViewById(R.id.tv_complete_percent);
        showDiaNoCancel();
    }

    public void setProgress(long percent) {

        tv_complete_percent.setText("" + percent);
//        progressBar.setProgress((int) percent);
    }

    // ================================Title=====================================

    /**
     * 不显示按钮
     *
     * @param clearHead
     * @param clearFoot
     */
    public void clear(boolean clearHead, boolean clearFoot) {

        if (clearHead) {
            re_head.setVisibility(View.GONE);
        }
        if (clearFoot) {
            re_foot.setVisibility(View.GONE);
        }
    }

    /**
     * 设置按钮文字
     *
     * @param left
     * @param right
     */
    public void setFoot(String left, String right) {

        btn_cancel.setText(left);
        btn_ok.setText(right);
    }

    /**
     * 设置按钮颜色，引导用户点击
     *
     * @param isLeft
     */
    public void setColour(boolean isLeft) {
        if (isLeft) {
            btn_cancel.setTextColor(context.getResources().getColor(
                    R.color.text_g));
        } else {
            btn_ok.setTextColor(context.getResources().getColor(R.color.text_g));
        }
    }

    /**
     * 设置标题
     *
     * @param text
     */
    public void setTitle(String text) {
        tv_diaTitle.setText(text);
    }

    /**
     * 设置标题，并且设置标题颜色
     *
     * @param text
     * @param colour
     */
    public void setTitle(String text, int colour) {
        tv_diaTitle.setText(text);
        tv_diaTitle.setTextColor(colour);
    }

    /**
     * 普通设置提示框
     *
     * @param text
     */
    public void setBody(String text) {
        tv_body.setText(text);
    }

    /**
     * 设置对话框的自定义界面
     *
     * @param resource
     */
    public void setTitle(int resource) {
        View v = View.inflate(context, resource, null);
        RelativeLayout re_title = (RelativeLayout) view
                .findViewById(R.id.re_title);
        re_title.removeAllViews();
        re_title.addView(v);
    }

    public void setBody(List<Map<String, String>> listData) {

        RelativeLayout re_body = (RelativeLayout) view
                .findViewById(R.id.re_body);
        re_body.removeAllViews();
        View bodyView = View
                .inflate(context, R.layout.common_dia_body_list, null);

        ListView listView = (ListView) bodyView.findViewById(R.id.lv_dia);

        SimpleAdapter adapter = new SimpleAdapter(context, listData,
                R.layout.common_dia_body_list_item, new String[]{"data"},
                new int[]{R.id.tv_dia_list_item});

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                listenser.onListViewItemClick(position, alertDialogType, code,
                        alertDialog);
                alertDialog.dismiss();
            }
        });

        listView.setAdapter(adapter);
        re_body.addView(bodyView);
    }

    /**
     * 该方法会显示提示的信息，
     *
     * @param textLong
     * @param text
     */
    public void setBody(final int textLong, String text, String judge) {
        this.judgeText = judge;
        setBody(textLong);
        tv_des.setVisibility(View.VISIBLE);
        tv_des.setText(text);
    }

    /**
     * 输入框界面，非数字
     *
     * @param textLong 限制输入字符串长度
     */
    public void setBody(int textLong) {
        setBody(textLong, false);
    }

    /**
     * 输入框界面,这个地方是判断删除
     *
     * @param textLong
     * @param isNumber 是否是数字
     */
    public void setBody(final int textLong, boolean isNumber) {

        this.textLong = textLong;
        RelativeLayout re_body = (RelativeLayout) view
                .findViewById(R.id.re_body);
        re_body.removeAllViews();
        View bodyView = View.inflate(context, R.layout.common_dia_edit, null);
        final EditText editText = (EditText) bodyView.findViewById(R.id.et_dia);
        tv_des = (TextView) bodyView.findViewById(R.id.tv_des);

        InputFilter[] filters = {new LengthFilter(textLong)};
        editText.setFilters(filters);

        if (isNumber) {
            editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        }
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {

                textData = s.toString();
                listenser.onTextChangeListenser(s.toString(), code);
                if (s.length() > 0) {

                    if (judgeText.equals("no")
                            || s.toString().equals(judgeText.trim())) {
                        setBtnRightClick();
                        setTextColour(true);
                    }
                } else {
                    btn_right.setClickable(false);
                    setTextColour(false);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        re_body.addView(bodyView);
    }

    /**
     * 列表界面
     *
     * @param data
     */
    public void setBody(String[] data) {
        RelativeLayout re_body = (RelativeLayout) view
                .findViewById(R.id.re_body);
        re_body.removeAllViews();
        View bodyView = View
                .inflate(context, R.layout.common_dia_body_list, null);

        ListView listView = (ListView) bodyView.findViewById(R.id.lv_dia);

        SimpleAdapter adapter = new SimpleAdapter(context, dataChanger(data),
                R.layout.common_dia_body_list_item, new String[]{"data"},
                new int[]{R.id.tv_dia_list_item});

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                alertDialog.dismiss();
                listenser.onListViewItemClick(position, alertDialogType, code,
                        alertDialog);
            }
        });

        listView.setAdapter(adapter);
        re_body.addView(bodyView);
    }

    /**
     * 列表界面
     *
     * @param data
     */
    public void setBodyChart(String[] data) {
        RelativeLayout re_body = (RelativeLayout) view
                .findViewById(R.id.re_body);
        re_body.removeAllViews();
        View bodyView = View.inflate(context,
                R.layout.commone_dia_body_list_chart, null);

        ListView listView = (ListView) bodyView.findViewById(R.id.lv_dia);

        SimpleAdapter adapter = new SimpleAdapter(context, dataChanger(data),
                R.layout.common_dia_body_list_item, new String[]{"data"},
                new int[]{R.id.tv_dia_list_item});

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                alertDialog.dismiss();
                listenser.onListViewItemClick(position, alertDialogType, code,
                        alertDialog);
            }
        });

        listView.setAdapter(adapter);
        re_body.addView(bodyView);
    }

    public void disMiss() {
        alertDialog.dismiss();
    }

    /**
     * 设置关闭按钮是否显示
     */
    public void setCloseVisiable() {
        re_back.setVisibility(View.VISIBLE);
    }

    private List<Map<String, String>> dataChanger(String[] data) {

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (String item : data) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("data", item);
            list.add(map);
        }
        return list;
    }

    private void setButtonClick() {
        btn_left = (Button) view.findViewById(R.id.btn_cancel);
        btn_right = (Button) view.findViewById(R.id.btn_ok);

        btn_left.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listenser.onNegetiveClick(textData);
                alertDialog.dismiss();
            }
        });
        btn_right.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listenser.onPositiveClick(textData);
                alertDialog.dismiss();
            }
        });

        if (alertDialogType == DialogueType.EDIT) {
            btn_right.setClickable(false);
            setTextColour(false);
        }
    }

    /**
     * 删除使用到的私有方法
     */
    private void setBtnRightClick() {
        setTextColour(true);
        btn_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                listenser.onPositiveClick(textData);
                listenser.onDialogueClick(R.id.btn_ok, alertDialogType, code,
                        alertDialog);
                alertDialog.dismiss();
            }
        });
    }

    /**
     * 设置右边字体颜色
     *
     * @param isGreen 是否是绿色
     */
    private void setTextColour(boolean isGreen) {
        if (isGreen) {
            btn_right.setTextColor(0xff0d91a0);
        } else {
            btn_right.setTextColor(0xff888888);
        }
    }

    public void registerListenser(MyDialogueUtilsListenser listenser) {
        this.listenser = listenser;
    }

    public class DialogueType {

        public static final int NORMAL = 1;
        public static final int EDIT = 2;
        public static final int LIST = 3;
    }
}
